package study.recursion;

public class MiGong {
    public static void main(String[] args) {
        int[][] map = createMap();
        findWay(map, 1, 1);
        printMap(map);

    }


    public static boolean findWay(int[][] map, int row, int col){
        if (map[6][5] == 2){    //到达终点
            return true;
        }else {
            if (map[row][col] == 0){    //当前这个点还没有走过
                map[row][col] = 2;  //往下走，标记为已走过
                if (findWay(map, row+1, col)){//向下走
                    return true;
                } else if (findWay(map, row, col+1)){//向右走
                    return true;
                }else if (findWay(map, row-1, col)){//向上走
                    return true;
                }else if (findWay(map, row, col-1)){//向左走
                    return true;
                }else {
                    //走不通
                    map[row][col] = 3;
                    return false;
                }
            }else { //表示 1，2，3的情况
                return false;
            }
        }
    }

    public static int[][] createMap(){
        int[][] map = new int[8][7];
        map[3][1] = 1;
        map[3][2] = 1;

        //设置上下挡板
        for (int i = 0; i < map[0].length; i++) {
            map[0][i] = 1;
            map[7][i] = 1;
        }
        //设置左右挡板
        for (int i = 0; i < map.length; i++) {
            map[i][0] = 1;
            map[i][6] = 1;
        }
        return map;
    }

    public static void printMap(int[][] map){
        for (int[] ints : map) {
            for (int j = 0; j < map[0].length; j++) {
                System.out.print(ints[j] + " ");
            }
            System.out.println();
        }
    }
}


